import java.io.*;
import java.net.*;
import java.util.*;

public class ConnectionProcessor2 implements Runnable
{
  private EM_Server2 server;

  private Socket socket;

  private DataInputStream din;

  private DataOutputStream dout;

  public ConnectionProcessor2( EM_Server2 server, Socket socket ) {
    this.server = server;
    this.socket = socket;

    new Thread( this ).start();
  }
  
  public void run() {
    try {
      InputStream in = socket.getInputStream();
      OutputStream out = socket.getOutputStream();

      din = new DataInputStream( in );
      dout = new DataOutputStream( out );

   
      //Conexión con la base de datos
      
      int id = din.readInt();
      String resultats = din.readUTF();
      try {
	GestorBD gestorbd = new GestorBD();
        gestorbd.modificar("em", "user", "password",
          "UPDATE em.EXPERIMENTS SET RESULTATS = '"+resultats+"', STATUS = 'Finalitzat' WHERE id = "+id+";");
      }
      catch (Exception e){e.printStackTrace();}
      
    } catch( IOException ie ) {
      try {
        socket.close();
      } catch( IOException ie2 ) {
        System.out.println( "Error closing socket "+socket );
      }

      server.removeConnection( this );

      System.out.println( "Closed connection from socket "+socket );
    }
  }

}
